package com.ruoyi.base.controller;

import com.ruoyi.base.domain.Warehouse;
import com.ruoyi.base.service.IWarehouseService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 仓库管理Controller
 *
 * @author ruoyi
 * @date 2023-04-28
 */
@RestController
@RequestMapping("/base/warehouse")
public class WarehouseController extends BaseController {
    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 查询仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:warehouse:list')")
    @GetMapping("/list")
    public TableDataInfo list(Warehouse warehouse) {
        startPage();
//        System.out.println(warehouse);
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
//        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出仓库管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:warehouse:export')")
    @Log(title = "仓库管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Warehouse warehouse) {
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);

        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        util.exportExcel(response, list, "仓库管理数据");
    }

    /**
     * 获取仓库管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:warehouse:query')")
    @GetMapping(value = "/{WarehouseID}")
    public AjaxResult getInfo(@PathVariable("WarehouseID") Long WarehouseID) {
        return success(warehouseService.selectWarehouseByWarehouseID(WarehouseID));
    }

    /**
     * 新增仓库管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouse:add')")
    @Log(title = "仓库管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Warehouse warehouse) {
        warehouse.setCreateUserID(getUserId());
        warehouse.setCreateUserName(getUsername());
        return toAjax(warehouseService.insertWarehouse(warehouse));
    }

    /**
     * 修改仓库管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouse:edit')")
    @Log(title = "仓库管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Warehouse warehouse) {
        warehouse.setEditUserID(getUserId());
        warehouse.setEditUserName(getUsername());
        return toAjax(warehouseService.updateWarehouse(warehouse));
    }

    /**
     * 删除仓库管理
     */
    @PreAuthorize("@ss.hasPermi('base:warehouse:remove')")
    @Log(title = "仓库管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{WarehouseIDs}")
    public AjaxResult remove(@PathVariable Long[] WarehouseIDs) {
        return toAjax(warehouseService.deleteWarehouseByWarehouseIDs(WarehouseIDs));
    }
}
